package com.controller;

import com.alibaba.fastjson.JSONObject;
import com.entity.User;
import com.github.pagehelper.PageInfo;
import com.service.IUserService;
import com.util.UID;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.annotation.Resources;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author Milburn
 * @create 2021-03-02 12:27
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;

    @RequestMapping("login")
    @ResponseBody
    public JSONObject login(String user_name, String user_pwd, HttpServletRequest request) {
        JSONObject obj = new JSONObject();
        User user = userService.login(user_name, user_pwd);
        if (user != null) {
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            if (user.getUser_role() == 0) {
                obj.put("msg", "usersuccess");
            } else {
                obj.put("msg", "adminsuccess");
            }
            obj.put("data", user);
            return obj;
        }
        obj.put("msg", "fail");
        return obj;

    }

    @ResponseBody
    @RequestMapping("logout")
    public String logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return "index";
    }

    @ResponseBody
    @RequestMapping("register")
    public String register(User user) {
        List<User> list = userService.findUserByName(user.getUser_name());
        if (list.size() > 0) {
            return "fail";//用户名存在
        } else {
            Integer rs = userService.addUser(user);
            if (rs > 0) {
                return "success";
            } else return "fail";
        }

    }

    @ResponseBody
    @RequestMapping("updateUser")
    public String updateUser(User user) {
        Integer rs = userService.updateUserInfo(user);
        if (rs > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping("modifyUser")
    private String modifyUserPwd(@RequestParam("oldPwd") String oldPwd,
                                 @RequestParam("newPwd") String newPwd,
                                 HttpServletRequest request) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user.getUser_pwd().equals(oldPwd)) {
            user.setUser_pwd(newPwd);
            userService.updateUserInfo(user);
            session.removeAttribute("user");
            return "success";
        } else {
            return "fail";
        }
    }

    @RequestMapping("findAllUser")
    @ResponseBody
    public JSONObject findAllUser(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                  @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                  String keyword
    ) {

        PageInfo<User> info = userService.findAllUsersBySplitPage(page, limit, keyword);
        JSONObject object = new JSONObject();
        object.put("msg", "");
        object.put("code", 0);
        object.put("count", info.getTotal());
        object.put("date", info.getList());
        return object;
    }

    @RequestMapping("findAllUserInfos")
    @ResponseBody
    public JSONObject findAllUserInfos() {
        JSONObject obj = new JSONObject();
        List<User> list = userService.findAllUsersInfos();
        obj.put("msg", "");
        obj.put("code", 0);
        obj.put("count", list.size());
        obj.put("data", list);
        return obj;
    }

    @RequestMapping("findUserInfosByName")
    @ResponseBody
    public JSONObject findUserInfosByName(@RequestParam("user_name") String user_name) {
        JSONObject obj = new JSONObject();
        List<User> list = userService.findUserLikeName(user_name);
        obj.put("msg", "");
        obj.put("code", 0);
        obj.put("count", list.size());
        obj.put("data", list);
        return obj;
    }

    @ResponseBody
    @RequestMapping("uploadHeadImg")
    public JSONObject uploadHeadImg(@RequestParam(value = "file", required = true) MultipartFile file,
                                    User user,
                                    HttpServletRequest request) throws IOException {
        JSONObject object = new JSONObject();
        if (file != null) {
            String str = file.getOriginalFilename();
            System.out.println("file" + str);
            String name = UID.getUID() + str.substring(str.lastIndexOf("."));
            System.out.println("name" + name);
            String path = request.getServletContext().getRealPath("/upload/head") + "/" + name;
            System.out.println("path" + path);
            String filePath = "../upload/head" + name;
            user.setUser_headImg(filePath);
            file.transferTo(new File(path));
            System.out.println("文件上传成功" + path);
        } else {
            user.setUser_headImg(this.userService.findUserById(user.getUser_id()).getUser_headImg());
            user.setUser_pwd(this.userService.findUserById(user.getUser_id()).getUser_pwd());
        }
        Integer rs = userService.updateUserInfo(user);
        if (rs > 0) {
            object.put("msg", "");
            object.put("code", 0);
            object.put("data", user);
        } else {
            object.put("msg", "");
            object.put("code", 200);
        }
        return object;
    }

}
